package com.gamatechno.solodestination;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.LocalActivityManager;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.gamatechno.adapter.ImageLoader;
import com.gamatechno.solodestination.model.AppPreferences;
import com.gamatechno.solodestination.model.HttpUtils;
import com.gamatechno.solodestination.model.Wisata;

@SuppressLint("ValidFragment")
public class SubWisataActivity extends FragmentActivity {
	public String judul;
	RatingBar rb;
	String tipe = "";
	String Lat, Lon;
	private TabHost mTabHost;
	private View mRoot;
	private ScrollView scrl;
	TextView SubTitle;
	Spinner spin;
	AppPreferences _pref;
	TextView tvdesc;
	ImageView imgHead;
	ImageLoader imgloader;
	TextView tvkategori;
	TextView tvkategori2;

	public SubWisataActivity(String tipes) {
		tipe = tipes;
	}

	public SubWisataActivity() {

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_subitem);
		Bitmap b = BitmapFactory.decodeResource(getResources(),
				R.drawable.header_img);
		BitmapDrawable bd = new BitmapDrawable(getResources(), b);
		ActionBar ab = getActionBar();
		ab.setBackgroundDrawable(bd);
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.actionbar_logo, null);
		ImageView iconMap = (ImageView) findViewById(R.id.iconmap);
		iconMap.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri
						.parse("http://maps.google.com/maps?daddr=" + Lat + ","
								+ Lon));
				startActivity(i);
			}
		});
		ab.setCustomView(v);
		ab.setDisplayShowCustomEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setLogo(R.drawable.ic_home);
		getActionBar().setTitle("");
		// mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
		Intent main = getIntent();
		tipe = main.getExtras().getString("judul");
		Log.i("tipe", tipe);
		_pref = new AppPreferences(this);
		SubTitle = (TextView) this.findViewById(R.id.titleSub);
		spin = (Spinner) this.findViewById(R.id.spinner);
		scrl = (ScrollView) this.findViewById(R.id.scrl);
		tvdesc = (TextView) this.findViewById(R.id.tvdesc);
		imgHead = (ImageView) this.findViewById(R.id.imgHead);
		tvkategori = (TextView) this.findViewById(R.id.tvkategori);
		// super.onActivityCreated(savedInstanceState);
		// LocalActivityManager mLocalActivityManager = new
		// LocalActivityManager(
		// this, false);
		// mLocalActivityManager.dispatchCreate(savedInstanceState);
		// mTabHost.setup(mLocalActivityManager);

		imgloader = new ImageLoader(this);
		new GetDetailWisata().execute();

	}

	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// _pref = new AppPreferences(this);
		// SubTitle = (TextView) this.findViewById(R.id.titleSub);
		// spin = (Spinner) this.findViewById(R.id.spinner);
		// scrl = (ScrollView) this.findViewById(R.id.scrl);
		// tvdesc = (TextView) this.findViewById(R.id.tvdesc);
		// imgHead = (ImageView) this.findViewById(R.id.imgHead);
		// tvkategori = (TextView) this.findViewById(R.id.tvkategori);
		// super.onActivityCreated(savedInstanceState);
		// LocalActivityManager mLocalActivityManager = new
		// LocalActivityManager(
		// this, false);
		// mLocalActivityManager.dispatchCreate(savedInstanceState);
		// mTabHost.setup(mLocalActivityManager);
		// new GetDetailWisata().execute();
		// setisi();
	}

	private class GetDetailWisata extends AsyncTask<Void, Void, Wisata> {

		ProgressDialog dialog = new ProgressDialog(SubWisataActivity.this);

		protected void onPreExecute() {
			dialog.setMessage(" Loading... ");
			dialog.show();
		}
		
		@Override
		protected Wisata doInBackground(Void... params) {
			Wisata kul = new Wisata();
			ArrayList<Wisata> ark = new ArrayList<Wisata>();
			String url = "http://sms.gamatechno.com/solo_destination/services/getWisata.php?method=wisatadetail&format=json&language="
					+ _pref.getBahasa() + "&id=" + tipe;
			Log.i("url", url);
			try {
				HashMap<String, String> hasash = new HashMap<String, String>();

				HttpUtils u = new HttpUtils();
				String a = u.getContents(url);
				Log.i("content", a);
				JSONObject obj = new JSONObject(a);
				Log.i("JSONObject content length", obj.length() + "");
				String jo = obj.get("result") + "";
				Log.i("isi result", jo + "");
				JSONObject obj2 = new JSONObject(jo);
				Log.i("JSONObject obj2 length", jo.length() + "");
				Log.i("nama", obj2.getString("name") + "");

				Log.i("i", obj2.getString("name"));
				Log.i("i", obj2.getString("description"));
				Log.i("i", obj2.getString("lat"));
				Log.i("i", obj2.getString("lng"));
				Log.i("i",
						"http://sms.gamatechno.com/solo_destination/files/wisata/"
								+ obj2.getString("images"));
				Log.i("i", obj2.getString("category"));
				Log.i("i", obj2.getString("district_id"));
				Log.i("i", obj2.getString("district"));
				// Log.i("i",obj2.getString("rating"));
				// Log.i("i",obj2.getString("comment"));
				Log.i("i", tipe);

				kul.setName(obj2.getString("name"));
				kul.setId_wisata(tipe);
				kul.setDescription(obj2.getString("description"));
				kul.setLat(obj2.getString("lat"));
				kul.setLng(obj2.getString("lng"));
				kul.setImg("http://sms.gamatechno.com/solo_destination/files/wisata/"
						+ obj2.getString("images"));
				kul.setDistrict(obj2.getString("district"));
				kul.setDistrict_id(obj2.getString("district_id"));
				kul.setCategory(obj2.getString("category"));

			} catch (Exception e) {
				// e.printStackTrace();
				Log.i("i", e.toString());
			}

			return kul;

		}

		@Override
		protected void onPostExecute(Wisata kul) {
			dialog.dismiss();
			setisi(kul);
			// setSuhu();
			// super.onPostExecute(result);
		}
	}

	public void setisi(Wisata kul) {

		SubTitle.setText(kul.getName());
		tvdesc.setText(Html.fromHtml(kul.getDescription()));
		imgloader.DisplayImage(kul.getImg(), imgHead);
		tvkategori.setText(kul.getCategory());
		// Intent i;
		// i = new Intent(this, Tab.class);
		// i.putExtra("i", "1");
		// TabSpec sp = mTabHost.newTabSpec("Tab1");
		// View viw = LayoutInflater.from(this).inflate(R.layout.tabs_bg,
		// null);
		// TextView tv_tb = (TextView) viw.findViewById(R.id.tabsText);
		// tv_tb.setText("Semua Komentar");
		// sp.setIndicator(viw);
		// sp.setContent(i);
		// mTabHost.addTab(sp);
		//
		// Intent j;
		// j = new Intent(this, Tab.class);
		// j.putExtra("i", "2");
		// TabSpec sp2 = mTabHost.newTabSpec("Tab2");
		// View viw2 = LayoutInflater.from(this).inflate(
		// R.layout.tabs_bg, null);
		// TextView tv_tb2 = (TextView) viw2.findViewById(R.id.tabsText);
		// tv_tb2.setText("Komentar Teman");
		// sp2.setIndicator(viw2);
		// sp2.setContent(j);
		// mTabHost.addTab(sp2);

		// mTabHost.addTab(sp);

		// scrl.pageScroll(View.FOCUS_UP);
	}

	// SEARCH ICON AND CLICKABLE
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);

		// Associate searchable configuration with the SearchView
		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
				.getActionView();
		searchView.setSearchableInfo(searchManager
				.getSearchableInfo(getComponentName()));

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// toggle nav drawer on selecting action bar app icon/title
		// Handle action bar actions click
		switch (item.getItemId()) {
		case R.id.action_search:
			return true;
		case android.R.id.home:
			this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// if nav drawer is opened, hide the action items
		SearchView mSearch = (SearchView) menu.findItem(R.id.action_search)
				.getActionView();
		int searchimg = getResources().getIdentifier(
				"android:id/search_button", null, null);
		ImageView v = (ImageView) mSearch.findViewById(searchimg);
		v.setImageResource(R.drawable.ic_action_search);
		// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		menu.findItem(R.id.action_search).setVisible(true);
		return super.onPrepareOptionsMenu(menu);
	}

}
